﻿Imports Microsoft.VisualBasic
Imports JPC.Medialibrary.BLL
Imports SubSonic

Namespace JPC.Medialibrary.DAL
    Partial Public Class Song

        Private _artistName As String
        Private _albumName As String
        Private _tags As String

        Public Property ArtistName() As String
            Get
                If _artistName = Nothing Then
                    Dim _artist As New Artist
                    _artist.LoadByKey(Me.ArtistId)
                    _artistName = _artist.ArtistName
                End If
                Return _artistName
            End Get

            Set(ByVal value As String)
                _artistName = value
            End Set
        End Property

        Public ReadOnly Property ArtistNameProper() As String
            Get
                Return MedialibraryManager.ProperName(Me.ArtistName)
            End Get

        End Property

        Public ReadOnly Property TitleProper() As String
            Get
                Return MedialibraryManager.ProperName(Me.Title)
            End Get

        End Property

        Public ReadOnly Property AlbumNameProper() As String
            Get
                Return MedialibraryManager.ProperName(Me.AlbumName)
            End Get

        End Property

        Public Property AlbumName() As String
            Get
                If _albumName = Nothing Then
                    Dim _album As New Album
                    _album.LoadByKey(Me.AlbumId)
                    _albumName = _album.AlbumName
                End If
                Return _albumName
            End Get

            Set(ByVal value As String)
                _albumName = value
            End Set
        End Property

        Public Property Tags() As String
            Get
                If _tags = Nothing Then
                    Dim tagList As List(Of Tag) = _
                        New [Select](Tag.TagIdColumn, Tag.TagNameColumn) _
                        .From(Tables.SongTag) _
                        .InnerJoin(Tables.Tag) _
                        .Where(SongTag.SongIdColumn).IsEqualTo(Me.SongId) _
                        .ExecuteTypedList(Of Tag)()

                    _tags = ""
                    For Each _tag As Tag In tagList
                        _tags = _tags & IIf(_tags = "", "", ",") & _tag.TagName.Trim
                    Next
                End If
                Return _tags
            End Get

            Set(ByVal value As String)
                _tags = value
            End Set
        End Property

        Public Sub IncrementPlayCount()

            Me.LastPlayed = Date.Today
            If IsNothing(Me.NumberOfPlays) Then
                Me.NumberOfPlays = 1
            Else
                Me.NumberOfPlays += 1
            End If
            Me.Save()

            If Me.ArtistId <> 0 Then
                Dim _artist As New Artist
                _artist.LoadByKey(Me.ArtistId)
                _artist.LastPlayed = Date.Today
                If IsNothing(_artist.NumberOfPlays) Then
                    _artist.NumberOfPlays = 1
                Else
                    _artist.NumberOfPlays += 1
                End If
                _artist.Save()
            End If

            If Me.AlbumId <> 0 Then
                Dim _album As New Album
                _album.LoadByKey(Me.AlbumId)
                _album.LastPlayed = Date.Today
                If IsNothing(_album.NumberOfPlays) Then
                    _album.NumberOfPlays = 1
                Else
                    _album.NumberOfPlays += 1
                End If
                _album.Save()
            End If

        End Sub

    End Class

End Namespace